{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "42eace26869295fb",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Console AppBuilderClient使用示例\n",
    "\n",
    "整体使用流程包括以下两个环节：\n",
    "\n",
    "1. 在[百度智能云千帆AppBuilder官网](https://cloud.baidu.com/product/AppBuilder)创建并发布应用、获取应用ID、获取密钥\n",
    "2. 引用AppBuilderSDK代码，初始化AppBuilderClient实例、创建会话、上传文档（可选）、执行对话\n",
    "\n",
    "以下分别提供三个样例，快递查询小助手、植物识别小助手、篮球教练来说明使用流程，注意以下流程用到的密钥可在图示位置中获取：\n",
    "<img src=\"./app_builder_resources/secret.png\" alt=\"drawing\" width=\"1000\"/>\n",
    "\n",
    "\n",
    "## 1. 快递查询小助手\n",
    "\n",
    "[<span style=\"font-size:1.3em;\"> 快递查询小助手</span>](https://appbuilder.baidu.com/s/KVSXK)一键体验\n",
    "\n",
    "### 1.1 Console端应用配置与发布\n",
    "1 进入[百度智能云千帆AppBuilder官网](https://cloud.baidu.com/product/AppBuilder)，配置智能体指令、选择相应的工具组件、发布应用\n",
    "\n",
    "<img src=\"./app_builder_resources/express_assistant.png\" alt=\"drawing\" width=\"1200\"/>\n",
    "\n",
    "2 获取应用ID\n",
    "<img src=\"./app_builder_resources/app_id.png\" alt=\"drawing\" width=\"1200\"/>\n",
    "\n",
    "### 1.2 SDK代码调用示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6a9e2ba6159a8e4",
   "metadata": {
    "collapsed": false,
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "import appbuilder\n",
    "import os\n",
    "# 注意以下示例正确运行依赖的条件包括：\n",
    "# 1. 在百度智能云千帆AppBuilder官网使用AppBuilderClient创建应用且应用已发布\n",
    "# 2. 密钥正确有效\n",
    "# 3. 密钥需要与发布应用正确对应，即需要使用发布应用的账户下的密钥\n",
    "\n",
    "# 配置密钥与应用ID\n",
    "os.environ[\"APPBUILDER_TOKEN\"] =\"secret_key\"\n",
    "app_id = \"35f4fed3-d530-4dad-bc8e-f2150a4450be\"\n",
    "\n",
    "# 初始化Agent\n",
    "builder = appbuilder.AppBuilderClient(app_id)\n",
    "\n",
    "# 创建会话ID\n",
    "conversation_id = builder.create_conversation()\n",
    "\n",
    "# 执行对话\n",
    "msg = builder.run(conversation_id, \"我的快递单号是：9858485940100; 请查询下此快递的状态\")\n",
    "print(\"快递查询助理回答内容：\", msg.content.answer)\n",
    "\n",
    "# 执行流式对话\n",
    "msg = builder.run(conversation_id, \"使用语音播报快递当前状态\", stream=True)\n",
    "for content in msg.content:\n",
    "    for ev in content.events:\n",
    "        if ev.content_type == \"audio\":\n",
    "            print(\"快递查询助理生成的音频播放地址：\", ev.detail[\"audio\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e462991dab3283d",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 2. 植物识别小助手\n",
    "[<span style=\"font-size:1.3em;\">植物识别小助手</span>](https://appbuilder.baidu.com/s/50zyD)一键体验\n",
    "\n",
    "### 2.1 Console端应用配置与发布\n",
    "1 进入[百度智能云千帆AppBuilder官网](https://cloud.baidu.com/product/AppBuilder)，配置智能体指令、选择相应的工具组件、发布应用\n",
    "<img src=\"./app_builder_resources/plant_recog_assistant.png\" alt=\"drawing\" width=\"1200\"/>\n",
    "\n",
    "2 获取应用ID, 同1.1\n",
    "\n",
    "### 2.2 SDK代码调用示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d051395a2aa1d11",
   "metadata": {
    "collapsed": false,
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import appbuilder\n",
    "\n",
    "# 注意以下示例正确运行依赖的条件包括：\n",
    "# 1. 在百度智能云千帆AppBuilder官网使用AppBuilderClient创建应用且应用已发布\n",
    "# 2. 密钥正确有效\n",
    "# 3. 密钥需要与发布的应用正确对应，即需要使用发布应用的账户下的密钥\n",
    "\n",
    "# 配置密钥与应用ID\n",
    "os.environ[\"APPBUILDER_TOKEN\"] =\"...\"\n",
    "app_id = \"7016e0d3-451b-4a47-a818-dc0a16d4b496\" \n",
    "\n",
    "# 初始化Agent实例\n",
    "builder = appbuilder.AppBuilderClient(app_id)\n",
    "\n",
    "# 创建会话ID\n",
    "conversation_id = builder.create_conversation()\n",
    "\n",
    "# 上传植物图片\n",
    "file_id = builder.upload_local_file(conversation_id, \"./app_builder_resources/tree.png\" )\n",
    "\n",
    "# 植物识别\n",
    "msg = builder.run(conversation_id, \"请识别图中的植物类别\", file_ids=[file_id])\n",
    "print(\"植物识别助理回答内容：\", msg.content.answer)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c112f09d6ce35d10",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 3. 篮球教练\n",
    "[<span style=\"font-size:1.3em;\">篮球教练知识增强检索</span>](https://appbuilder.baidu.com/s/RCVEn)一键体验\n",
    "\n",
    "### 3.1 Console端应用配置与发布\n",
    "1 进入[百度智能云千帆AppBuilder官网](https://cloud.baidu.com/product/AppBuilder)，配置智能体指令、上传文档并关联知识库、发布应用\n",
    "<img src=\"./app_builder_resources/agent_builder_rag.png\" alt=\"drawing\" width=\"1200\"/>\n",
    "\n",
    "2 获取应用ID, 同1.1\n",
    "\n",
    "### 3.2 SDK代码调用示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65e488aa883cf94e",
   "metadata": {
    "collapsed": false,
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import appbuilder\n",
    "\n",
    "# 注意以下示例正确运行依赖的条件包括：\n",
    "# 1. 在百度智能云千帆AppBuilder官网使用AppBuilderClient创建应用且应用已发布\n",
    "# 2. 密钥正确有效\n",
    "# 3. 密钥需要与发布的应用正确对应，即需要使用发布应用的账户下的密钥\n",
    "\n",
    "# 配置密钥与应用ID\n",
    "os.environ[\"APPBUILDER_TOKEN\"] =\"...\"\n",
    "app_id = \"4316a7cb-b6b2-4448-b6fa-ff131c484ec9\" \n",
    "\n",
    "# 初始化Agent实例\n",
    "builder = appbuilder.AppBuilderClient(app_id)\n",
    "\n",
    "# 创建会话ID\n",
    "conversation_id = builder.create_conversation()\n",
    "\n",
    "# 执行对话\n",
    "msg = builder.run(conversation_id, \"突破技巧中如何运用胯下变向？\", )\n",
    "print(\"篮球教练回答内容：\", msg.content.answer)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
